package com.edgclub.edg.utils.util;

import android.app.Activity;

import java.util.Stack;

 /**
  * activity的同意管理
  *
  * @author 陈坤彬
  *
  */
 public class ActivityManager {

     private Stack<Activity> activities;

     private static ActivityManager instance;
     private Activity currentActivity;

     private ActivityManager() {
         activities = new Stack<Activity>();
     }

     /**
      * singleton
      *
      * @return
      */
     public static ActivityManager getInstance() {
         if (null == instance) {
             synchronized (ActivityManager.class) {
                 if (null == instance) {
                     instance = new ActivityManager();
                 }
             }
         }

         return instance;
     }

     /**
      * 添加
      *
      * @param activity
      */
     public void addActivity(Activity activity) {
         if (null != activities && !activities.contains(activity)){
             activities.add(activity);
             currentActivity=activity;
         }else if(null != activities&& activities.contains(activity)){
             currentActivity=activity;
         }
     }

     /**
      * 移除
      *
      * @param activity
      */
     public void removeActivity(Activity activity) {
         if (null != activities && activities.contains(activity)) {
             activities.remove(activity);
             currentActivity=null;
         }
     }

     /**
      * 销毁
      *
      * @param activity
      */
     public void finishActivity(Activity activity) {
         removeActivity(activity);
         activity.finish();
         currentActivity=null;
     }

     /**
      * 结束全部activity 相当于停止应用
      */
     public void finishAll() {
         for (int i = activities.size() - 1; i >= 0; i--) {
             finishActivity(activities.elementAt(i));
         }
         currentActivity=null;
     }

     /**
      * 结束全部activity 相当于停止应用
      */
     public void finishAllWithoutTop() {
         for (int i = activities.size() - 1; i > -1; i--) {
             finishActivity(activities.elementAt(i));
         }
         currentActivity=null;
     }

     /**
      * 结束除了主页之外的所有页面
      */
     public void finishAllWithoutMain() {
         for (int i = activities.size() - 1; i > 0; i--) {
             finishActivity(activities.elementAt(i));
         }
     }

     public Stack<Activity> getActivities() {
         return activities;
     }

     public void setActivities(Stack<Activity> activities) {
         this.activities = activities;
     }

 }
